package com.atguigu.config;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration  // 配置类
@EnableSwagger2 // swagger注解
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                // 接口文档的基本信息
                .apiInfo(webApiInfo())
                // 如何保护我们的Api，有三种验证(ApiKey, BasicAuth, OAuth)
                .securitySchemes(security())
                .select()
                // 方法需要有ApiOperation注解才能生存接口文档
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                // 路径使用any风格
                .paths(PathSelectors.any())
                // .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                // .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

    /**
     * 接口文档详细信息
     */
    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("MyBatis-Plus")
                .description("API接口文档")
                .version("1.0.1")
                .contact(new Contact("IT-Jie", "https://gitee.com/it-jie", "it_jie_520@163.com"))
                .build();
    }

    /**
     * 保护Api，通过ApiKey
     */
    private List<ApiKey> security() {
        List<ApiKey> result = new ArrayList<>();
        result.add(new ApiKey("token", "token", "header"));
        return result;
    }
}
